package tools;

import Entity.userBean;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.io.IOUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@WebServlet("/UploadImage2Servlet")
public class UploadImage2Servlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        DiskFileItemFactory factory = new DiskFileItemFactory();
        ServletFileUpload upload = new ServletFileUpload(factory);
        int flag = 1;
        List<FileItem> list = null;
        userBean ff = new userBean();
        String head_img = null;
        try {
            list = upload.parseRequest(request);
            for (FileItem fi : list ) {
                //判断是否是文本域
                if(!fi.isFormField()){
                    if(fi.getSize()!=0){ //判断文件上传框是否选择了图片
                        flag = 0;
                        System.out.println("图片开始上传");
                        head_img = fi.getName();
                        ff.setImg(head_img);
                        String path = "E:\\java_ee\\lastexperiment1.0\\web\\img";
                        InputStream is = fi.getInputStream();
                        System.out.println(is);
                        FileOutputStream os = new FileOutputStream(new File(path, head_img));
                        IOUtils.copy(is,os);
                        os.close();
                        is.close();
                    }
                }
                else{
                    //存储非文件流的信息
                    if(fi.getSize()!=0) {
                        String name = fi.getFieldName();
                        if (name.equals("name"))
                            //String name=new String(fi.getString()).getBytes("ISO-8859-1"),"UTF-8");
                            ff.setUsername(new String(fi.getString().getBytes("ISO-8859-1"),"UTF-8"));
                        if (name.equals("id"))
                            ff.setUse_id(new String(fi.getString().getBytes("ISO-8859-1"),"UTF-8"));
                        if (name.equals("sex"))
                            ff.setUse_id(new String(fi.getString().getBytes("ISO-8859-1"),"UTF-8"));
                        if (name.equals("phone"))
                            ff.setUse_id(new String(fi.getString().getBytes("ISO-8859-1"),"UTF-8"));
                        if (name.equals("address"))
                            ff.setUse_id(new String(fi.getString().getBytes("ISO-8859-1"),"UTF-8"));
                        }
                    }
                }
        } catch (FileUploadException e) {
            e.printStackTrace();
        }

        response.sendRedirect("http://localhost:8080/person_center.jsp");
    }
}
